package com.x.component.widget;

import com.x.component.connectivity.FrameworkServicesModule;

import android.app.ActivityOptions;
import android.app.WindowConfiguration;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.hardware.input.InputManager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class WindowWidgetHost {

	private static WindowWidgetHost mInstance = null;

	private static final Object mLock = new Object();

	private Context mContext;
	private DisplayManager mDisplayManager;
	private InputManager mInputManager;

	private VirtualDisplay virtualDisplay;
	
	private SurfaceView windowSurfaceView;

	private WindowWidgetHost() {

	}

	public static WindowWidgetHost getInstance() {
		if (null == mInstance) {
			synchronized (mLock) {
				if (null == mInstance) {
					mInstance = new WindowWidgetHost();
				}
			}
		}
		return mInstance;
	}

	public void init(Context context) {
		this.mContext = context;
		mDisplayManager = FrameworkServicesModule.provideDisplayManager(mContext);

	}

	public int createWindowWidget(String name,SurfaceView surfaceView, ComponentName compoent, int width, int height, int density,
			Surface surface) {
		if (TextUtils.isEmpty(name) || surfaceView == null || compoent == null || TextUtils.isEmpty(compoent.getPackageName())
				|| TextUtils.isEmpty(compoent.getClassName()) || width <= 0 || height <= 0 || density < 120) {
			return -1;
		}
		mDisplayManager = FrameworkServicesModule.provideDisplayManager(mContext);
		mInputManager = FrameworkServicesModule.provideInputManager(mContext);
		this.windowSurfaceView = surfaceView;
		this.windowSurfaceView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					windowSurfaceView.getParent().requestDisallowInterceptTouchEvent(true);
					break;

				default:
					break;
				}
				
				if(virtualDisplay != null ) {
	                event.setDisplayId(virtualDisplay.getDisplay().getDisplayId());
	                mInputManager.injectInputEvent(event,
	                        InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);	
				}
				return true;
			}
		});
		try {
			int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC
					| DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
					| DisplayManager.VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL
					| DisplayManager.VIRTUAL_DISPLAY_FLAG_TRUSTED;
			setVirtualDisplay(mDisplayManager.createVirtualDisplay(name, width, height, density, surface, flags));

			Intent mapIntent = new Intent();
			mapIntent.setAction(Intent.ACTION_MAIN);
			mapIntent.addCategory(Intent.CATEGORY_LAUNCHER);
			mapIntent.setPackage(compoent.getPackageName());
			mapIntent.setClassName(mContext, compoent.getClassName());
			mapIntent.setComponent(compoent);
			mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mapIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
			mapIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			ActivityOptions options = ActivityOptions.makeBasic();
			options.setLaunchDisplayId(getVirtualDisplay().getDisplay().getDisplayId());
			options.setLaunchWindowingMode(WindowConfiguration.WINDOWING_MODE_FULLSCREEN);
			mContext.startActivity(mapIntent, options.toBundle());
			return 0;
		} catch (Exception e) {
		}
		return -1;
	}
	
	public void releaseWindowWidget(String name) {
		if(virtualDisplay!=null) 
		{
			virtualDisplay.release();
			virtualDisplay = null;
		}
	}

	public VirtualDisplay getVirtualDisplay() {
		return virtualDisplay;
	}

	public int getVirtualDisplayId() {
		if(virtualDisplay==null) {
			return -1;
		}
		return virtualDisplay.getDisplay().getDisplayId();
	}

	private void setVirtualDisplay(VirtualDisplay virtualDisplay) {
		this.virtualDisplay = virtualDisplay;
	}

}
